package com.codi.bus.core.service.impl;

import com.codi.base.util.ExceptionUtil;
import com.codi.bus.core.dao.UserFeedbackDao;
import com.codi.bus.core.domain.UserFeedback;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.service.UserFeedbackService;
import com.codi.bus.core.service.model.UserFeedbackModel;
import com.codi.bus.exception.SystemDBException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @DESCRIBE
 * @Author sungaoxiang [sungaoxiang@cd121.com]
 * @DATE 2017/10/13 15:47
 **/
@Service("userFeedbackService")
@Transactional
public class UserFeedbackServiceImpl implements UserFeedbackService {

    private static final Logger logger = LoggerFactory.getLogger(UserFeedbackServiceImpl.class);

    @Resource
    private UserFeedbackDao userFeedbackDao;

    @Override
    public BaseResult addFeedback(UserFeedback model) {
        BaseResult back = new BaseResult();
        try {
            model.setCreateTime(new Date());
            userFeedbackDao.insertSelective(model);
            back.setSuccess(true);
            back.setResult(model.getFeedbackId());
        } catch (Exception e) {
            ExceptionUtil.logError(logger, "addFeedback - Exception:", e);
            throw new SystemDBException("addFeedback Failure!", e);
        }
        return back;
    }

    @Override
    public BaseResult updateFeedback(UserFeedback model) {
        BaseResult back = new BaseResult();
        try {
            model.setUpdateTime(new Date());
            userFeedbackDao.updateByPrimaryKeySelective(model);
            back.setSuccess(true);
            back.setResult(model.getFeedbackId());
        } catch (Exception e) {
            ExceptionUtil.logError(logger, "updateFeedback - Exception:", e);
            throw new SystemDBException("updateFeedback Failure!", e);
        }
        return back;
    }

    @Override
    public List<UserFeedback> queryList(Integer pageIndex, Integer pageSize, UserFeedbackModel model) {
        return userFeedbackDao.queryList(pageIndex, pageSize, model);
    }

    @Override
    public Integer queryCount(UserFeedbackModel model) {
        return userFeedbackDao.queryCount(model);
    }
}
